package com.ld.hpfs.web.controller;


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.dao.UtilMapper;
import com.ld.hpfs.pojo.ExtPager;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.LeftService;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class LeftController {
	private static final Logger logger = LoggerFactory.getLogger(LeftController.class);

	@Autowired
	private LeftService leftService;
	
	@Autowired
    private UtilMapper   utilMapper;
	
	@RequestMapping(value = "/getLeftList",method = RequestMethod.POST)
    public void getLeftList(ExtPager page, HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {	   
        response.setContentType("text/plain;charset=UTF-8");
        HashMap<String, Object> result = new HashMap<String, Object>();
        try {
        	 User user = (User)session.getAttribute(WebConstants.CURRENT_USER);
           	 HashMap<String,Object> param = new HashMap<String,Object>();
	         param.put("user_id", user.getUserId());
	         param.put("lang", session.getAttribute(WebConstants.CURRENT_LANG).toString());
             result.put("success", true);
             ArrayList<HashMap<String, Object>> logs =leftService.getLeftList(param);            
                if (logs != null && logs.size() > 0) {
                    result.put("data", logs);                  
                } else {
                    result.put("data", "");
                }	           
            String returnMsg = JackJson.fromObjectToJson(result);
            logger.debug("{}", returnMsg);
            writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
    }
	
	/**
	 * 得到首页消息框信息
	 */
	@RequestMapping(value = "/getMessageBoxList")
    public void getMessageBoxList(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {       	   
        response.setContentType("text/plain;charset=UTF-8");
        HashMap<String, Object> result = new HashMap<String, Object>();
        try {       	          	       	         
        	Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        	User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
       	 	String language = (String) session.getAttribute(WebConstants.CURRENT_LANG);
        	ArrayList<HashMap<String,Object>> messages =leftService.getMessageBoxList(Integer.parseInt(currentCompany.toString()), Integer.parseInt(user.getUserId()),language);  
        	result.put("data", messages);
            String returnMsg = JackJson.fromObjectToJson(result);
            logger.debug("{}", returnMsg);
            writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
    }
	
	
	/**
	 * 得到首页折线图(期间，导入数据量)信息
	 */
	@RequestMapping(value = "/getLineStoreList")
	public void getLineStoreList(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer){
		response.setContentType("text/plain;charset=UTF-8");
        try { 
        	HashMap<String,Object> param = new HashMap<String,Object>();
        	Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			param.put("companyId",Integer.parseInt(currentCompany.toString()));          
        	 ArrayList<HashMap<String,Object>> result =leftService.getLineStoreList(param);   
       // 	 result.put("data", list);
             String returnMsg = JackJson.fromObjectToJson(result);
             logger.debug("{}", returnMsg);
             writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
	}
	
	/**
	 * 得到首页横向柱状图(报表名称，生成进度)信息
	 */
	@RequestMapping(value = "/getDocTypeList")
	public void getDocTypeList(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer){
		response.setContentType("text/plain;charset=UTF-8");
        try { 
        	HashMap<String,Object> param = new HashMap<String,Object>();
        	String language = (String) session
					.getAttribute(WebConstants.CURRENT_LANG);
			param.put("language", language);
        	Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			param.put("companyId",Integer.parseInt(currentCompany.toString()));          
        	 ArrayList<HashMap<String,Object>> result =leftService.getDocTypeList(param);   
             String returnMsg = JackJson.fromObjectToJson(result);
             logger.debug("{}", returnMsg);
             writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
	}
	
	
	/**
	 * 得到首页竖向柱状图(用户，登录次数)信息
	 */
	@RequestMapping(value = "/getColumnStoreList")
	public void getColumnStoreList(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer){
		response.setContentType("text/plain;charset=UTF-8");
        try { 
        	HashMap<String,Object> param = new HashMap<String,Object>();
        	Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			param.put("companyId",Integer.parseInt(currentCompany.toString()));          
        	 ArrayList<HashMap<String,Object>> result =leftService.getColumnStoreList(param);   
             String returnMsg = JackJson.fromObjectToJson(result);
             logger.debug("{}", returnMsg);
             writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
	}
	
	/**
	 * 得到首页红废票折线图信息
	 */
	@RequestMapping(value = "/getRedDevilList")
	public void getRedDevilList(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer){
		response.setContentType("text/plain;charset=UTF-8");
        try { 
        	HashMap<String,Object> param = new HashMap<String,Object>();
        	Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        	User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	param.put("companyId",Integer.parseInt(currentCompany.toString()));          
        	param.put("userId", Integer.parseInt(user.getUserId()));
			ArrayList<HashMap<String,Object>> result =leftService.getRedDevilList(param);   
             String returnMsg = JackJson.fromObjectToJson(result);
             logger.debug("{}", returnMsg);
             writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
	}
	/**
	 * 得到饼图信息
	 */
	@RequestMapping(value = "/getColumnStoreCount")
	public void getColumnStoreCount(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer){
		response.setContentType("text/plain;charset=UTF-8");
        try { 
        	HashMap<String,Object> param = new HashMap<String,Object>();
        	String language = (String) session
					.getAttribute(WebConstants.CURRENT_LANG);
			param.put("language", language);
        	Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			param.put("companyId",Integer.parseInt(currentCompany.toString()));          
        	 ArrayList<HashMap<String,Object>> result =leftService.getColumnStoreCount(param);   
             String returnMsg = JackJson.fromObjectToJson(result);
             logger.debug("{}", returnMsg);
             writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
	}
	
	/**
	 * 得到首页饼图(导入成功，导入失败，生成凭证成功，生成凭证失败，过账成功，过账失败)信息
	 */
	@RequestMapping(value = "/getPieStoreList")
	public void getPieStoreList(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer){
		response.setContentType("text/plain;charset=UTF-8");
        try { 
        	HashMap<String,Object> param = new HashMap<String,Object>();
        	Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			param.put("companyId",Integer.parseInt(currentCompany.toString()));  
			param.put("lang", session.getAttribute(WebConstants.CURRENT_LANG).toString());
        	 ArrayList<HashMap<String,Object>> result =leftService.getPieStoreList(param);   
             String returnMsg = JackJson.fromObjectToJson(result);
             logger.debug("{}", returnMsg);
             writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
	}
	
	/*
	 * getInvoiceInitConfig 获取当前登录公司 票据设置 属性
	 */
	@RequestMapping(value = "/getInvoiceInitConfig")
    public void getInvoiceInitConfig(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {	   
        response.setContentType("text/plain;charset=UTF-8");
        HashMap<String, Object> result = new HashMap<String, Object>();
        try {
        	 HashMap<String,Object> param = new HashMap<String,Object>();
        	 Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			 param.put("companyId",Integer.parseInt(currentCompany.toString()));
             result.put("success", true);
             ArrayList<HashMap<String, Object>> logs =leftService.getInvoiceInitConfig(param);           
                if (logs != null && logs.size() > 0) {
                    result.put("data", logs);                  
                } else {
                    result.put("data", "");
                }	           
            String returnMsg = JackJson.fromObjectToJson(result);
            logger.debug("{}", returnMsg);
            writer.print(returnMsg);
        } catch (Exception e) {
            logger.error("Exception: {}", e);
        } finally {
            writer.flush();
            writer.close();
        }
    }
	
	
	  @RequestMapping(value = "/checkCreatorFromGroupId")
		public void checkCreatorFromGroupId(HttpSession session, HttpServletRequest request,HttpServletResponse response,PrintWriter writer){
			HashMap<String,Object> result = new HashMap<String,Object>();
			response.setContentType("text/plain;charset=UTF-8");
			try {
								
				int ret;			
				Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
				int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
		        int createCompanyId = utilMapper.getCreateCompanyIdFromGroupId(groupId);
		        if (createCompanyId != Integer.parseInt(currentCompany.toString()))
		        {
		            ret = 2;
		        }
		        else ret = 0;
		        
		        if(ret == 0){
					result.put("resultcode",0);
				}else if(ret == 2){
					result.put("resultcode", 2);
				}
				writer.print(JackJson.fromObjectToJson(result));
			} catch (Exception e) {
				logger.error("Exception: ", e);
			} finally {
				writer.flush();
				writer.close();
			}
		}
	  
	  @RequestMapping(value = "/getLineMaxValue")
		public void getLineMaxValue(HttpSession session,
				HttpServletRequest request, HttpServletResponse response,
				PrintWriter writer) {
			response.setContentType("text/plain;charset=UTF-8");
			HashMap<String, Object> param = new HashMap<String, Object>();
			try {
				
				// 设置参数
				param.put("companyId",
						session.getAttribute(WebConstants.CURRENT_COMPANY)
								.toString());
				int result = leftService.getLineMaxValue(param);
				logger.debug("{}", result);
				writer.print(result);
			} catch (Exception e) {
				logger.error("Exception: ", e);
			} finally {
				writer.flush();
				writer.close();
			}
		}
}